<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态表格</title>

    <style>
        table{
            border: 1px solid;
            margin: auto;
            width: 500px;
        }

        td, th {
            text-align: center;
            border: 1px solid;
        }

        div {
            text-align: center;
            margin: 50px;
        }
    </style>
</head>
<body>
    <div>
         <input type="text" id="id" placeholder="请输入编号">
         <input type="text" id="name" placeholder="请输入姓名">
         <input type="text" id="gender" placeholder="请输入性别">
         <input type="button" value="添加" id="btn_add">
    </div>

    <table >
        <caption>学生信息表</caption>
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>操作</th>
        </tr>

        <tr>
            <td>1</td>
            <td>令狐冲</td>
            <td>男</td>
            <td><a href="javascript:void(0);" onclick="delTr(this);">删除</a></td>
        </tr>

    </table>

    <script>
        //1.添加：
        //   1. 给添加按钮绑定单击事件
        //   通过元素id获取，并且onclick点击，再次通过匿名函数方法
       document.getElementById("btn_add").onclick = function () {
           //   2. 获取文本框的内容
           let id = document.getElementById("id").value;
           let name = document.getElementById("name").value;
           let gender = document.getElementById("gender").value;
           //   3. 创建td，设置td的文本为文本框的内容。
           //   createTextNode() 可创建文本节点

           //id
           let td_id = document.createElement("td");
           let text_id = document.createTextNode(id);
           td_id.appendChild(text_id);

           //name
           let td_name = document.createElement("td");
           let text_name = document.createTextNode(name);
           td_name.appendChild(text_name);

           //gender
           let td_gender = document.createElement("td");
           let text_gender = document.createTextNode(gender);
           td_gender.appendChild(text_gender);

           //a
           let td_a = document.createElement("td");
           let ele_a = document.createElement("a");
           //设置属性,
           // javascript:void(0)计算一个表达式但是不返回值,死链接(不会刷新当前页面)
           ele_a.setAttribute("href", "javascript:void(0)");
           ele_a.setAttribute("onclick", "delTr(this);")
           //createTextNode() 可创建文本节点
           let text_a = document.createTextNode("删除");

           //5.把text_a添加到ele_a
           ele_a.appendChild(text_a);
           td_a.appendChild(ele_a);

           //6.把td添加到tr
           let tr = document.createElement("tr");
           tr.appendChild(td_id);
           tr.appendChild(td_name);
           tr.appendChild(td_gender);
           tr.appendChild(td_a);

           //获取table
           let table = document.getElementsByTagName("table")[0];
           table.appendChild(tr);
       }
           //第二种  innerHTML添加
           document.getElementById("btn_add").onclick = function () {
               //获取文本框内容
               let id = document.getElementById("id").value;
               let name = document.getElementById("name").value;
               let gender = document.getElementById("gender").value;

               //获取table
               let table = document.getElementsByTagName("table")[0];

               table.innerHTML += "<tr>\n" +
                   "<td>" + id + "</td>\n" +
                   "<td>" + name + "</td>\n" +
                   "<td>" + gender + "</td>\n" +
                   "<td><a href=\"javascript:void(0);\" onclick=\"delTr(this);\" >删除</a></td>\n" +
                   "</tr>";
           }

           //删除
           function delTr(obj) {
               let tr = obj.parentNode.parentNode;
               let table = tr.parentNode;
               let flag = confirm("是否删除？");
               if (flag == true) {
                   table.removeChild(tr);
               }
           }
    </script>
</body>
</html>